package server;

import constants.GameConstants;
import constants.ServerConstants;
import database.DatabaseConnection;
import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import provider.MapleData;
import provider.MapleDataProvider;
import provider.MapleDataProviderFactory;
import provider.MapleDataTool;

public class CashItemFactory {

    private static CashItemFactory instance = new CashItemFactory();
    private static int[] bestItems = {GameConstants.GMS ? 10003055 : 10002819, GameConstants.GMS ? 10003090 : 50100010, GameConstants.GMS ? 10103464 : 50200001, GameConstants.GMS ? 10002960 : 10002147, GameConstants.GMS ? 10103363 : 60000073};
    private Map<Integer, CashItemInfo> itemStats = new HashMap();
    private Map<Integer, CashItemInfo> itemStatsForItemId = new HashMap();
    private Map<Integer, List<Integer>> itemPackage = new HashMap();
    private Map<Integer, CashItemInfo.CashModInfo> itemMods = new HashMap();
    private final Map<Integer, List<Integer>> openBox = new HashMap<>();
    private Map<Integer, CashItemInfo.CashModInfo> itemModsForItemId = new HashMap();
    private MapleDataProvider data = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Etc.wz"));
    private MapleData commodities = this.data.getData("Commodity.img");
    private final List<CashCategory> categories = new LinkedList<>();
    private Map<Integer, Integer> idLookup = new HashMap();
    private final Map<Integer, CashItem> categoryItems = new HashMap<>();
    private final Map<Integer, CashItem> menuItems = new HashMap<>();

    public static CashItemFactory getInstance() {
        return instance;
    }

    public void initialize_for_maplewing() {
        //*去除验证
        final List<MapleData> cccc = data.getData("Commodity.img").getChildren();
        for (MapleData field : cccc) {
            final int SN = MapleDataTool.getIntConvert("SN", field, 0);

            final CashItemInfo stats = new CashItemInfo(MapleDataTool.getIntConvert("ItemId", field, 0),
                    MapleDataTool.getIntConvert("Count", field, 1),
                    MapleDataTool.getIntConvert("Price", field, 0), SN,
                    MapleDataTool.getIntConvert("Period", field, 0),
                    MapleDataTool.getIntConvert("Gender", field, 2),
                    MapleDataTool.getIntConvert("OnSale", field, 0) > 0 && MapleDataTool.getIntConvert("Price", field, 0) > 0,
                    0);

            if (SN > 0) {
                itemStats.put(SN, stats);
            }
        }

        final MapleData b = data.getData("CashPackage.img");
        for (MapleData c : b.getChildren()) {
            if (c.getChildByPath("SN") == null) {
                continue;
            }
            final List<Integer> packageItems = new ArrayList<>();
            for (MapleData d : c.getChildByPath("SN").getChildren()) {
                packageItems.add(MapleDataTool.getIntConvert(d));
            }
            itemPackage.put(Integer.parseInt(c.getName()), packageItems);
        }

        try {
            Connection con = DatabaseConnection.getConnection();
            try (PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_modified_items"); ResultSet rs = ps.executeQuery()) {
                if (rs.next()) {
                    CashItemInfo.CashModInfo ret = new CashItemInfo.CashModInfo(rs.getInt("serial"), rs.getInt("discount_price"), rs.getInt("mark"), rs.getInt("showup") > 0, rs.getInt("itemid"), rs.getInt("priority"), rs.getInt("package") > 0, rs.getInt("period"), rs.getInt("gender"), rs.getInt("count"), rs.getInt("meso"), rs.getInt("unk_1"), rs.getInt("unk_2"), rs.getInt("unk_3"), rs.getInt("extra_flags"));
                    itemMods.put(ret.sn, ret);
                    if (ret.showUp) {
                        final CashItemInfo cc = itemStats.get(Integer.valueOf(ret.sn));
                        if (cc != null) {
                            ret.toCItem(cc); //init
                        }
                    }
                }
            }
        } catch (Exception e) {
        }

        List<Integer> availableSN = new LinkedList<>();
        availableSN.add(20001141);
        availableSN.add(20001142);
        availableSN.add(20001143);
        availableSN.add(20001144);
        availableSN.add(20001145);
        availableSN.add(20001146);
        availableSN.add(20001147);
        openBox.put(5533003, availableSN); // Rainbow Visor Box

        availableSN = new LinkedList<>();
        availableSN.add(20000462);
        availableSN.add(20000463);
        availableSN.add(20000464);
        availableSN.add(20000465);
        availableSN.add(20000466);
        availableSN.add(20000467);
        availableSN.add(20000468);
        availableSN.add(20000469);
        openBox.put(5533000, availableSN); // Korean stuffs..

        availableSN = new LinkedList<>();
        availableSN.add(20800259);
        availableSN.add(20800260);
        availableSN.add(20800263);
        availableSN.add(20800264);
        availableSN.add(20800265);
        availableSN.add(20800267);
        openBox.put(5533001, availableSN); // Angelic Beam Weapon Box

        availableSN = new LinkedList<>();
        availableSN.add(20800270);
        availableSN.add(20800271);
        availableSN.add(20800272);
        availableSN.add(20800273);
        availableSN.add(20800274);
        openBox.put(5533002, availableSN); // Chief Knight Weapon Box

        try {
            Connection con = DatabaseConnection.getConnection();
            try (PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_categories"); ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    CashCategory cat = new CashCategory(rs.getInt("categoryid"), rs.getString("name"), rs.getInt("parent"), rs.getInt("flag"), rs.getInt("sold"));
                    categories.add(cat);
                }
            }
        } catch (Exception e) {
            System.out.println("Failed to load cash shop categories. " + e);
        }

        try {
            Connection con = DatabaseConnection.getConnection();
            try (PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_menuitems"); ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    CashItem item = new CashItem(rs.getInt("category"), rs.getInt("subcategory"), rs.getInt("parent"), rs.getString("image"), rs.getInt("sn"), rs.getInt("itemid"), rs.getInt("flag"), rs.getInt("price"), rs.getInt("discountPrice"), rs.getInt("quantity"), rs.getInt("expire"), rs.getInt("gender"), rs.getInt("likes"));
                    menuItems.put(item.getSN(), item);
                }
            }
        } catch (Exception e) {
            System.out.println("Failed to load cash shop categories. " + e);
        }

        try {
            Connection con = DatabaseConnection.getConnection();
            try (PreparedStatement ps = con.prepareStatement("SELECT * FROM cashshop_items"); ResultSet rs = ps.executeQuery()) {
                while (rs.next()) {
                    CashItem item = new CashItem(rs.getInt("category"), rs.getInt("subcategory"), rs.getInt("parent"), rs.getString("image"), rs.getInt("sn"), rs.getInt("itemid"), rs.getInt("flag"), rs.getInt("price"), rs.getInt("discountPrice"), rs.getInt("quantity"), rs.getInt("expire"), rs.getInt("gender"), rs.getInt("likes"));
                    categoryItems.put(item.getSN(), item);
                }
            }
        } catch (Exception e) {
            System.out.println("Failed to load cash shop categories. " + e);
        }

    }

    public CashItemInfo getSimpleItem(int sn) {
        return (CashItemInfo) this.itemStats.get(Integer.valueOf(sn));
    }

    public CashItemInfo getItem(int sn) {
        CashItemInfo stats = (CashItemInfo) this.itemStats.get(Integer.valueOf(sn));
        CashItemInfo.CashModInfo z = getModInfo(sn);

        if ((z != null) && (z.showUp)) {
            return z.toCItem(stats);
        }
        if ((stats == null) || (!stats.onSale())) {
            return null;
        }

        return stats;
    }

    public CashItemInfo getSimpleItemForItemId(int itemid) {
        return (CashItemInfo) this.itemStatsForItemId.get(Integer.valueOf(itemid));
    }

    public CashItemInfo getItemForItemId(int itemid) {
        CashItemInfo stats = (CashItemInfo) this.itemStatsForItemId.get(Integer.valueOf(itemid));
        CashItemInfo.CashModInfo z = getModInfoForItemId(itemid);

        if ((z != null) && (z.showUp)) {
            return z.toCItem(stats);
        }
        if ((stats == null) || (!stats.onSale())) {
            return null;
        }

        return stats;
    }

    public List<Integer> getPackageItems(int itemId) {
        return (List) this.itemPackage.get(Integer.valueOf(itemId));
    }

    public CashItemInfo.CashModInfo getModInfo(int sn) {
        return (CashItemInfo.CashModInfo) this.itemMods.get(Integer.valueOf(sn));
    }

    public CashItemInfo.CashModInfo getModInfoForItemId(int itemid) {
        return (CashItemInfo.CashModInfo) this.itemModsForItemId.get(Integer.valueOf(itemid));
    }

    public Collection<CashItemInfo.CashModInfo> getAllModInfo() {
        return this.itemMods.values();
    }

    public Collection<CashItemInfo.CashModInfo> getAllModInfoForItemId() {
        return this.itemModsForItemId.values();
    }

    public int[] getBestItems() {
        return bestItems;
    }

    public int getSnFromId(int itemId) {
        return ((Integer) this.idLookup.get(Integer.valueOf(itemId))).intValue();
    }
}

/* Location:           C:\Users\文炳\Desktop\CXFBY\114Maplewing.jar
 * Qualified Name:     server.CashItemFactory
 * JD-Core Version:    0.6.2
 */